<template>
  <div class="cate">
    <breadcrumb :first="'商品管理'"></breadcrumb>
    <el-card>
      <el-button @click="openAddCate" class="add" type="primary"
        >添加分类</el-button
      >

      <tree-table
        show-index
        border
        :selection-type="false"
        :expand-type="false"
        :data="catelist"
        :columns="columns"
      >
        <template slot="isflag" slot-scope="scope">
          <i
            v-if="scope.row.cat_deleted === false"
            class="el-icon-success"
            style="color: skyblue"
          ></i>
          <i v-else class="el-icon-error" style="color: red"></i>
        </template>
        <template slot="islevel" slot-scope="scope">
          <el-tag v-if="scope.row.cat_level === 0">一级</el-tag>
          <el-tag v-else-if="scope.row.cat_level === 1" type="success"
            >二级</el-tag
          >
          <el-tag v-else type="warning">三级</el-tag>
        </template>
        <template slot="isopt" slot-scope="scope">
          <el-button type="primary" size="mini">编辑</el-button>
          <el-button @click="delcate(scope.row)" type="danger" size="mini"
            >删除</el-button
          >
        </template>
      </tree-table>
      <!-- <pagination></pagination> -->
      <el-pagination
        @current-change="handleCurrentChange"
        :page-size="catedata.pagesize"
        layout="total, prev, pager, next"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <!-- 添加分类弹出框 -->
    <el-dialog title="添加分类" :visible.sync="cateflag" @close="cateform = {} , cascaderValue = []">
        <!-- 表单 -->
      <el-form
        :model="cateform"
        :rules="cateformrule"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="cateform.cat_name"></el-input>
        </el-form-item>
        <!-- 级联选择器 -->
        <el-form-item label="父级分类">
          <el-cascader
            clearable
            change-on-select
            v-model="cascaderValue"
            :options="catelist2"
            expandTrigger="hover"
            :props="cascaderProps"
            @change="handleChange"
          ></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cateflag = false">取 消</el-button>
        <el-button type="primary" @click="addCate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import breadcrumb from "@/components/breadcrumb.vue";
import { categorieslist, delcategories ,addcategories} from "@/api/index";
export default {
  data() {
    return {
      //商品分类数据列表
      catelist: [],
      catelist2: [],
      //查询条件
      catedata: {
        type: 3,
        pagenum: 1,
        pagesize: 5,
      },
      //总条数
      total: 0,
      columns: [
        //   {
        //     label: '#',
        //     prop: 'cat_id',
        //   },
        {
          label: "分类名称",
          prop: "cat_name",
        },
        {
          label: "是否有效",
          prop: "cat_deleted",
          type: "template",
          //当前这列使用的模板名称
          template: "isflag",
        },
        {
          label: "排序",
          prop: "cat_level",
          type: "template",
          template: "islevel",
        },
        {
          label: "操作",
          type: "template",
          template: "isopt",
        },
      ],
      //对话弹框开关
      cateflag: false,
      //表单数据
      cateform: {
        cat_pid: 0,
        cat_name: "",
        cat_level: 0,
      },
      //表单规则校验
      cateformrule: {
        cat_name: [
          { required: true, message: "请输入分类名称", trigger: "blur" },
        ],
      },
      //级联选择器props属性
      cascaderProps: {
        value: 'cat_id',
        label: 'cat_name',
        children: 'children'
      },
      //级联选择器的值
      cascaderValue: []
    };
  },
  components: {
    breadcrumb,
  },
  created() {
    this.getCateList();
  },
  methods: {
    getCateList() {
      categorieslist(this.catedata).then((res) => {
        console.log(res.data);
        this.catelist = res.data.data.result;
        this.total = res.data.data.total;
      });
    },
    handleCurrentChange(val) {
      this.catedata.pagenum = val;
      this.getCateList();
    },
    delcate(row) {
      delcategories(row.cat_id).then((res) => {
        if (res.data.meta.status !== 200) {
          return this.$message.error(res.data.meta.msg);
        }
        this.getCateList();
        
      });
    },
    openAddCate() {
      this.cateflag = true;
      categorieslist({ type: 2 }).then((res) => {
        this.catelist2 = res.data.data;
      });
    },
    addCate() {
        addcategories(this.cateform).then( res=>{
            if(res.data.meta.status !== 201){
                return this.$message.error(res.data.meta.msg)
            }
            this.getCateList();
            this.$message({
                message: '添加成功',
                type: 'success'
            })
        })
      this.cateflag = false;
    },
    handleChange(){
        console.log(this.cascaderValue);
        if(this.cascaderValue.length > 0){
            //父级分类的ID
            this.cateform.cat_pid = this.cascaderValue[this.cascaderValue.length-1]
            //分类等级
            this.cateform.cat_level = this.cascaderValue.length
        } else{
            this.cateform.cat_pid = 0
            this.cateform.cat_level = 0
        }
    }
  },
};
</script>

<style lang="less" scoped>
.cate {
  width: 100%;
  height: 100%;
  box-sizing: border-box;
  padding: 10px;
  .add {
    margin-bottom: 10px;
  }
}
</style>
